package com.example.qingyiapp.ui.activity.fragment.fragment_FindUI;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.qingyiapp.R;
import com.example.qingyiapp.ui.activity.fragment.fragment_FindUI.adapter.QuestionListViewAdapter;
import com.example.qingyiapp.ui.activity.fragment.fragment_FindUI.bean.Question;
import com.example.qingyiapp.ui.activity.fragment.fragment_FindUI.bean.QuestionDA;
import com.example.qingyiapp.ui.activity.fragment.fragment_FindUI.utils.MyListView;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class QuestionActivity extends AppCompatActivity {

    @BindView(R.id.find_question_Relayout)
    RelativeLayout findQuestionRelayout;
    @BindView(R.id.find_question_title)
    TextView findQuestionTitle;
    @BindView(R.id.find_question_Icon)
    ImageView findQuestionIcon;
    @BindView(R.id.find_question_userName)
    TextView findQuestionUserName;
    @BindView(R.id.find_question_time)
    TextView findQuestionTime;
    @BindView(R.id.find_question_content)
    TextView findQuestionContent;
    @BindView(R.id.myview)
    View myview;
    @BindView(R.id.find_question_listView)
    MyListView findQuestionListView;
    @BindView(R.id.find_question_back)
    ImageView findQuestionBack;
    String stringExtra;
    @BindView(R.id.find_question_image)
    ImageView findQuestionImage;

    ArrayList<QuestionDA.DataBean> list;
    QuestionListViewAdapter adapter;
    @BindView(R.id.find_question_shuliang)
    TextView findQuestionShuliang;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question);
        ButterKnife.bind(this);

        list = new ArrayList<>();
        Intent intent = getIntent();
        stringExtra = intent.getStringExtra("qa_id");
        adapter = new QuestionListViewAdapter(list, this);
        findQuestionListView.setAdapter(adapter);

        getData();
        getData2();

        initLinestener();
    }

    private void initLinestener() {
        findQuestionBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    public void getData2() {

        Request.Builder builder = new Request.Builder();
        builder.url("http://www.iqingyi.com/qa/getQuestionAnswers?startidx=0&num=10&question_id=" + stringExtra);
        builder.get();
        Request build = builder.build();
        OkHttpClient client = new OkHttpClient();
        client.newCall(build).enqueue(new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                ResponseBody body = response.body();
                String string = body.string();
                Gson gson = new Gson();
                QuestionDA questionDA = gson.fromJson(string, QuestionDA.class);

                final List<QuestionDA.DataBean> data = questionDA.getData();
                for (int i = 0; i < data.size(); i++) {
                    list.add(data.get(i));
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        findQuestionShuliang.setText("全部回答("+data.size()+")");

                        adapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }


    public void getData() {


        Request.Builder builder = new Request.Builder();
        builder.url("http://www.iqingyi.com/qa/getQuestionDetail?question_id=" + stringExtra);
        builder.get();
        Request build = builder.build();
        OkHttpClient client = new OkHttpClient();
        client.newCall(build).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                ResponseBody body = response.body();
                String string = body.string();
                Gson gson = new Gson();
                final Question question = gson.fromJson(string, Question.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        findQuestionTitle.setText(question.getData().getTitle() + "");
                        Glide.with(QuestionActivity.this).load(question.getData().getUser().getUsercover() + "").apply(RequestOptions.circleCropTransform()).into(findQuestionIcon);
                        findQuestionUserName.setText(question.getData().getUser().getName() + "");
                        findQuestionTime.setText(question.getData().getTime().split(" ")[0] + "");
                        if (question.getData().getImagestd() != null) {
                            findQuestionImage.setVisibility(View.VISIBLE);
                            Glide.with(QuestionActivity.this).load(question.getData().getImagecover() + "").into(findQuestionImage);

                        } else {
                            findQuestionImage.setVisibility(View.GONE);
                        }
                        findQuestionContent.setText(question.getData().getContent() + "");

                    }
                });


            }
        });
    }
}
